"use strict";
// 交易模块数据获取
import Reflux from "reflux";

import lscache from "lscache";

import { ClientActions,ClientDetailActions } from "../action/clientaction.js";
import { StoreMixin } from "./common.js";
import { getISODate } from "../common/datehelper.js";
import { ajaxCall } from "../common/apihelper.js";


var ClientStore = Reflux.createStore({
    // 获取交易页面所有数据
    mixins: [ StoreMixin ],
    listenables: [ClientActions],
    cacheKey: 'stores.clientstore',
    params: {
        sign:'ABB89E0ADAE0B1B16D6E06DBE95ACF1E', 
        dimdate:'DAY', 
        startdate: getISODate(new Date()).replace(/\-/g, ''), 
        enddate: getISODate(new Date()).replace(/\-/g, ''),
        ds_customer_t1_tb_comparetype: 'YOY',
        ds_customer_t1_hb_comparetype: 'DOD',
        needback: 1,

        // 总客户，流失资产特殊处理
        ds_customer_t1_tb_day_dimdate: 'DAY', 
        ds_customer_t1_tb_day_comparetype: 'YOY',
        ds_customer_t1_hb_day_dimdate: 'DAY', 
        ds_customer_t1_hb_day_comparetype: 'DOD',
        ds_customer_t1_tb_day_startdate: getISODate(new Date()).replace(/\-/g, ''), 
        ds_customer_t1_tb_day_enddate: getISODate(new Date()).replace(/\-/g, ''),
        ds_customer_t1_hb_day_startdate: getISODate(new Date()).replace(/\-/g, ''), 
        ds_customer_t1_hb_day_enddate: getISODate(new Date()).replace(/\-/g, ''),       
    },
    apis: {
        // ds_customer_t0: {params: ['sign']},

        ds_customer_t1_tb: {name:'ds_customer_t1', params: ['sign', 'dimdate', 'startdate', 'enddate', 'comparetype', 'needback']},
        ds_customer_t1_hb: {name:'ds_customer_t1', params: ['sign', 'dimdate', 'startdate', 'enddate', 'comparetype', 'needback']},     

        ds_customer_t1_tb_day: {name:'ds_customer_t1', params: ['sign', 'dimdate', 'startdate', 'enddate', 'comparetype', 'needback']},
        ds_customer_t1_hb_day: {name:'ds_customer_t1', params: ['sign', 'dimdate', 'startdate', 'enddate', 'comparetype', 'needback']}     
    },
    apis_customer: {
        ds_customer_volumes: {params: ['sign', 'dimdate', 'startdate', 'enddate', 'needback']}
    },
    apis_newacct: {
        ds_newacct_volumes: {params: ['sign', 'dimdate', 'startdate', 'enddate', 'needback']}
    },
    apis_newcust: {
        ds_newcust_volumes: {params: ['sign', 'dimdate', 'startdate', 'enddate', 'needback']}
    },
    apis_newcon: {
        ds_newcon_volumes: {params: ['sign', 'dimdate', 'startdate', 'enddate', 'needback']}
    },
    apis_newasset: {
        ds_newasset_volumes:{params: ['sign', 'dimdate', 'startdate', 'enddate', 'needback']}
    },
    apis_closeacc: {
        ds_closeacc_volumes:{params: ['sign', 'dimdate', 'startdate', 'enddate', 'needback']},
    },
    apis_lostass: {
        ds_lostass_volumes:{params: ['sign', 'dimdate', 'startdate', 'enddate', 'needback']},
    },
    apis_asset: {
        ds_asset_volumes:{params: ['sign', 'dimdate', 'startdate', 'enddate', 'needback']},
    },
    onT0Fetch: function(callback1){
        // 获取 T0 数据
        var callback = function(response){
            if (callback1) {
                callback1();
                setTimeout(function() {
                    this.pack('ds_customer_t0', response);         
                }.bind(this), 100);
            } else {
                this.pack('ds_customer_t0', response);
            }
        }.bind(this);

        ajaxCall('/v1/exchange/data/query/ds_customer_t0', {}, callback);
    },
    fakeData: function(api_name, params){
        // 伪造数据, 测试用
        //var key = api_name + hash.MD5(params);
        var data = null;
        var pdata = null;
        // switch(api_name){
        // }
        if (pdata != null && data == null) {
            data = new Array();
            var cols = pdata[0];
            var rows = pdata.slice(1);
            _(rows).forOwn(function(row){
                data.push(_.zipObject(cols, row));
            }).value();
        }
        //lscache.set(key, data, 10);
        this.pack(api_name, data);
    }

});

var ClientDetailStore = Reflux.createStore({
    // 获取交易页面所有数据
    mixins: [ StoreMixin ],
    listenables: [ClientDetailActions],
    cacheKey: 'stores.clientdetailstore',
    params: {
        sign:'ABB89E0ADAE0B1B16D6E06DBE95ACF1E', 
        dimdate:'DAY', 
        startdate: '20150922', 
        enddate: '20150925'
    },
    apis: {
        
    },
    apis_customer: {
        'ds_customer_volumes': {params: ['sign', 'dimdate', 'startdate', 'enddate']},
        'ds_customer_brch_rank': {params: ['sign', 'dimdate', 'startdate', 'enddate']},
        'ds_customer_area_rank': {params: ['sign', 'dimdate', 'startdate', 'enddate']}
    },
    apis_newacct: {
        'ds_newacct_volumes': {params: ['sign', 'dimdate', 'startdate', 'enddate']},
        'ds_newacct_brch_rank': {params: ['sign', 'dimdate', 'startdate', 'enddate']},
        'ds_newacct_area_rank': {params: ['sign', 'dimdate', 'startdate', 'enddate']}
    },
    apis_newcust: {
        'ds_newcust_volumes': {params: ['sign', 'dimdate', 'startdate', 'enddate']},
        'ds_newcust_brch_rank': {params: ['sign', 'dimdate', 'startdate', 'enddate']},
        'ds_newcust_area_rank': {params: ['sign', 'dimdate', 'startdate', 'enddate']}
    },
    apis_newcon: {
        'ds_newcon_volumes': {params: ['sign', 'dimdate', 'startdate', 'enddate']},
       // 'ds_newcon_brch_rank': {params: ['sign', 'dimdate', 'startdate', 'enddate']},
       // 'ds_newcon_area_rank': {params: ['sign', 'dimdate', 'startdate', 'enddate']}
    },
    apis_newasset: {
        'ds_newasset_volumes': {params: ['sign', 'dimdate', 'startdate', 'enddate']},
        'ds_newasset_brch_rank': {params: ['sign', 'dimdate', 'startdate', 'enddate']},
        'ds_newasset_area_rank': {params: ['sign', 'dimdate', 'startdate', 'enddate']}
    },
    apis_closeacc: {
        'ds_closeacc_volumes': {params: ['sign', 'dimdate', 'startdate', 'enddate']},
        'ds_closeacc_brch_rank': {params: ['sign', 'dimdate', 'startdate', 'enddate']},
        'ds_closeacc_area_rank': {params: ['sign', 'dimdate', 'startdate', 'enddate']}
    },
    apis_lostass: {
        'ds_lostass_volumes': {params: ['sign', 'dimdate', 'startdate', 'enddate']},
        'ds_lostass_brch_rank': {params: ['sign', 'dimdate', 'startdate', 'enddate']},
        'ds_lostass_area_rank': {params: ['sign', 'dimdate', 'startdate', 'enddate']}
    },
    apis_asset: {
        'ds_asset_volumes': {params: ['sign', 'dimdate', 'startdate', 'enddate']},
        'ds_asset_brch_rank': {params: ['sign', 'dimdate', 'startdate', 'enddate']},
        'ds_asset_area_rank': {params: ['sign', 'dimdate', 'startdate', 'enddate']}
    },
    apis_rate: {
        'ds_rate_volumes': {params: ['sign', 'dimdate', 'startdate', 'enddate']},
        'ds_rate_brch_rank': {params: ['sign', 'dimdate', 'startdate', 'enddate']},
        'ds_rate_area_rank': {params: ['sign', 'dimdate', 'startdate', 'enddate']}
    },

    fakeData: function(api_name, params){
        // 伪造数据, 测试用
        //var key = api_name + hash.MD5(params);
        var data = null;
        var pdata = null;
        // switch(api_name){
 
        // }
        if (pdata != null && data == null) {
            data = new Array();
            var cols = pdata[0];
            var rows = pdata.slice(1);
            _(rows).forOwn(function(row){
                data.push(_.zipObject(cols, row));
            }).value();
        }
        //lscache.set(key, data, 10);
        this.pack(api_name, data);
    }

});
export { ClientStore, ClientDetailStore };